%**************************************************************************
%
%           "Endogenous Liquidity and Capital Reallocation"
%      Cui, W., Wright, R., & Zhu, Y. (2024), Journal of Political Economy
%                       Last Modified: Feb 2024.  
%
%**************************************************************************

%%%% Comparative statics with tax policy

clc; clear; close all; format short

%% 1. --------------------      Initilization    --------------------------

%%%% this step is important to initiate if parameters were not saved
% Main_iid_calibration

tic
%%%% load parameters from calibration
load param_calibrated; 
load eqm_calibrated


%%%% Set up grids for tau_k and THETA
variable_x          = '\tau_k'; 
l_bound             = - 1.2;
u_bound             = 0.25;
Num_grid_1          = 50;
Num_grid_2          = 20;
range               = [l_bound, u_bound];
tau_k_grid          = [linspace(l_bound, -0.95, Num_grid_1), linspace(-0.94, u_bound, Num_grid_2)];
tau_k_grid          = fliplr(tau_k_grid);
THETA_grid          = [0.45, 0.50, 0.55];
THETA_grid_legend   = {'0.45','0.50','0.55'};
colors              = [0, 0, 1; 1, 0, 0; 0.9, 0.6, 0.2];
line_style          = {'--';'-';'-.'};

%%%% Options value to be used in fsolve
options                     = optimset('Display','off'); 
options.OptimalityTolerance = 1e-6; 
options.StepTolerance       = 1e-6;
options.MaxIter             = 1000000; 
options.MaxFunEvals         = 1000000; 
options.FunctionTolerance   = 1e-6;


%% 2. ---------------  Now, running comparative statics ------------------
disp('*********************> Comparative Statics <***********************')
par_ini = par_calibrated;
do_model_1_and_2; % needs the benchmark (Hansen Model)

plotting_ = 1; % plot the result in do_model_6

par_ini = par_calibrated;
do_model_6

%%%% welfare maximizing capital tax

if strcmp(variable_x,'\tau_k')
    
    for ii = 1:length(THETA_grid)
        fun_temp = @(Xn) interp1(x_grid(:,ii), -Welfare_model_6(:,ii), Xn, 'spline');
        [tau_k_opt, Welfare_model_6_opt] = fminsearch(@(x) fun_temp(x), 0.02);
        fprintf('Welfare maximizing capital tax for THETA = %.2f is %.8f \n\n', THETA_grid(ii), tau_k_opt)
    end
    
end

%% 3. ---------------  optimal capital tax when iota=0 and tau_h=0 -------- 
disp('*********************> Comparative Statics Again <*****************')
disp('Optimal tax when iota = 0, tau_h = 0, and theta = 0.5')
THETA_grid          = 0.50;
plotting_           = 0; % plot the result in do_model_6

par_ini             = par_calibrated;
par_ini.iota        = 0.00001;
par_ini.tau_h       = 0;

do_model_6

%%%% welfare maximizing capital tax
if strcmp(variable_x,'\tau_k')
    
    for ii = 1:length(THETA_grid)
        fun_temp = @(Xn) interp1(x_grid(:,ii), -Welfare_model_6(:,ii), Xn, 'spline');
        [tau_k_opt, Welfare_model_6_opt] = fminsearch(@(x) fun_temp(x), 0.02);
        fprintf('Welfare maximizing capital tax for THETA = %.2f is %.8f \n\n', THETA_grid(ii), tau_k_opt)
    end
    
end

toc

